package com.gt.aep.base.entity.util;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 接口返回通用
 * @param <T>
 */
@Data
public class Result<T> {
    private Integer code;//返回码
    private String msg;//返回消息
    private T data;//返回的数据


    /**
     * 返回成功，返回消息，无数据返回
     * @param msg
     * @return
     */
    public static Map<String,Object> success(String msg){
        Result result = new Result();
        Map<String,Object> map=new HashMap<>();
        result.setCode(0);
        result.setMsg(msg);
        map.put("msg",result.getMsg());
        map.put("code",result.getCode());
        return map;
    }

    /**
     * 返回成功，返回自定义键值对
     * @param key
     * @param value
     * @return
     */
    public static Map<String,Object> success(String key,String value){
        Result result = new Result();
        Map<String,Object> map=new HashMap<>();
        result.setCode(0);
        map.put("code",result.getCode());
        map.put(key,value);
        return map;
    }

    /**
     * 返回成功，返回数据
     * @param object
     * @return
     */
    public static Map<String,Object> success(Object object){
        Result result = new Result();
        Map<String,Object> map=new HashMap<>();
        result.setCode(0);
        result.setData(object);
        map.put("code",result.getCode());
        map.put("data",result.data);
        return map;
    }

    /**
     * 返回空map
     * @return
     */
    public static Map<String,Object> success(){
        return success(null,null);
    }

    /**
     * 返回失败，返回失败信息
     * @param msg
     * @return
     */
    public static Map<String,Object> error(String msg){
        Result result = new Result();
        Map<String,Object> map=new HashMap<>();
        result.setCode(1);
        result.setMsg(msg);
        result.setData(null);
        map.put("msg",result.getMsg());
        map.put("code",result.getCode());
        return map;
    }

    /**
     * 返回失败，失败信息为空
     * @return
     */
    public static Map<String,Object> error(){
        Result result = new Result();
        Map<String,Object> map=new HashMap<>();
        result.setCode(1);
        result.setMsg(null);
        result.setData(null);
        map.put("msg",result.getMsg());
        map.put("code",result.getCode());
        return map;
    }
}
